package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.IYzrStorehouseDAO;
import com.trkj.dao.jpa.LmbConsumptiveStockDAO;
import com.trkj.dao.jpa.LmbCousumablePurchaseDAO;
import com.trkj.dao.jpa.LmbMaterialStorageDAO;
import com.trkj.dao.jpa.LmbMaterialStorageDetailDAO;
import com.trkj.dao.jpa.LmbPurchaseDetailDAO;
import com.trkj.dao.jpa.LmbSupplierDAO;
import com.trkj.dao.jpa.LmbTbstaffDAO;
import com.trkj.pojos.LmbConsumptiveStock;
import com.trkj.pojos.LmbCousumablePurchase;
import com.trkj.pojos.LmbMaterialStorage;
import com.trkj.pojos.LmbMaterialStorageDetail;
import com.trkj.pojos.LmbPurchaseDetail;
import com.trkj.pojos.Tbstaff;
import com.trkj.pojos.YzrStorehouse;
import com.trkj.pojos.YzrSupplier;
import com.trkj.vo.LmbMaterialStorageDetailVo;
import com.trkj.vo.LmbMaterialStorageVo;
import com.trkj.vo.LmbMetStorageVo;

@Service
@Transactional
public class LmbMaterialStorageService {
	@Autowired 
	private LmbMaterialStorageDAO lmbMatStorageDAO;
	@Autowired
	private LmbMaterialStorageDetailDAO lmbMatStorageDetDAO;
	@Autowired
	private LmbPurchaseDetailDAO purDetailDAO;
	@Autowired
	private LmbConsumptiveStockDAO consumptiveStockDAO;
	@Autowired
	private LmbTbstaffDAO lmbtbstaffDAO;
	@Autowired
	private IYzrStorehouseDAO storehouseDAO;
	@Autowired
	private LmbSupplierDAO supplierDAO;
	@Autowired
	private LmbCousumablePurchaseDAO couPurDAO;
	
	/**
	 * 耗材入库
	 * @param matmStorageVo
	 */
	public void doHr(LmbMaterialStorageVo matmStorageVo){		
		LmbOddNumbersService oddNumbersService = new LmbOddNumbersService();
		LmbMaterialStorage matStorage = matmStorageVo.getMatStorage();
		LmbCousumablePurchase couPurchase = couPurDAO.findOne(matStorage.getMsVoucherId());
		String rkStatus = couPurchase.getCpRsStatus();
		if(!rkStatus.equals("已入库")){
			//获取日期
			String date = oddNumbersService.rq();
			//拼接
			String name = "HR-" + date;					
			String n = lmbMatStorageDAO.findMaxID(name);	
			System.out.println("从数据库中读取的最大采购计划编号："+n);
			String mid = oddNumbersService.getId(name, n);
			System.out.println(1);	
			//保存入库单
			matStorage.setMsId(mid);
			matStorage.setMsDate(new Date());
			lmbMatStorageDAO.save(matStorage);
			//保存入库状态
			couPurchase.setCpRsStatus("已入库");
			couPurDAO.save(couPurchase);
			//保存入库详情			
			System.out.println(matStorage.getMsVoucherId());		
			List<LmbPurchaseDetail> list = new ArrayList<LmbPurchaseDetail>();
			list=purDetailDAO.findDetail(matStorage.getMsVoucherId());
			System.out.println("llllist.size()"+list.size());		
			System.out.println(list.size());
			int i =0;
			if(list!=null){
				for(LmbPurchaseDetail purchaseDetail:list ){
					System.out.println("耗材名称为："+purchaseDetail.getPdName());
				}
				List<LmbMaterialStorageDetail> msd= copyMsdlist(list);	
				System.out.println("msd.size():"+msd.size());
				for(LmbMaterialStorageDetail matStorageDet:msd){
					//拼接
					String name1 = "HRX-" + date;
					String n1 = lmbMatStorageDetDAO.findMaxID(name1);
					String mdid = oddNumbersService.getId(name1, n1);
					matStorageDet.setMsdId(mdid);
					matStorageDet.setMsdMsId(mid);
					lmbMatStorageDetDAO.save(matStorageDet);
					LmbConsumptiveStock ConsumptiveStock = consumptiveStockDAO.findConStock(matStorageDet.getMsdCId(), matStorageDet.getMsdBatchNum());
					if(ConsumptiveStock==null){
						//拼接
						String name2 = "HK-" + date;
						String n2 = consumptiveStockDAO.findMaxID(name2);
						String sid = oddNumbersService.getId(name2, n2);
						LmbConsumptiveStock cs = new LmbConsumptiveStock();
						cs.setCstockId(sid);
						cs.setCstocBatchNum(matStorageDet.getMsdBatchNum());
						cs.setCstocCid(matStorageDet.getMsdCId());
						cs.setCstocCname(matStorageDet.getMsdCName());
						cs.setCstockSupplier(matStorageDet.getMsdSupplierl());
						cs.setCstockValidTill(matStorageDet.getMsdValidTill());
						cs.setCstockProductDate(matStorageDet.getMsdProductDate());
						cs.setCstocNum(matStorageDet.getMsdNum());
						cs.setCstocNorms(matStorageDet.getMsdNorms());
						cs.setCstocWarehouseId(matStorage.getMsStatus());
						consumptiveStockDAO.save(cs);
					}else{
						long num = ConsumptiveStock.getCstocNum().longValue() + matStorageDet.getMsdNum().longValue();
						ConsumptiveStock.setCstocNum(new BigDecimal(num));
					}	
					System.out.println(i);
					i=i+1;
					System.out.println("下一个为:"+i);
				}
			}	
		}			
	}	
	
	
	private LmbMaterialStorageDetail copyMaterialStorageDetail(LmbPurchaseDetail src1){
		LmbMaterialStorageDetail desc1 = new LmbMaterialStorageDetail();
		desc1.setMsdCId(src1.getPdCId());
		System.out.println("src1:"+src1.getPdName());
		desc1.setMsdCName(src1.getPdName());
		System.out.println("desc1:"+desc1.getMsdCName());
		desc1.setMsdBatchNum(src1.getPdBatchNum());
		desc1.setMsdNorms(src1.getPdNorms());
		desc1.setMsdNum(src1.getPdNum());
		desc1.setMsdProductDate(src1.getPdProductDate());
		desc1.setMsdValidTill(src1.getPdValidTill());
		desc1.setMsdSupplierl(src1.getPdSupplier());	
		return desc1;
	}
	
	private List<LmbMaterialStorageDetail> copyMsdlist(List<LmbPurchaseDetail> list){
		if(list == null) return null;
		List<LmbMaterialStorageDetail> listVo = new ArrayList<>();
		for(LmbPurchaseDetail purDetail: list){
			LmbMaterialStorageDetail msd = copyMaterialStorageDetail(purDetail);
			listVo.add(msd);
		}
		return listVo;	
	}
	
	public LmbMetStorageVo metStoragePoToVo(LmbMaterialStorage matStorage){
		LmbMetStorageVo metStorageVo = new LmbMetStorageVo();
		metStorageVo.setMsId(matStorage.getMsId());
		System.out.println("699"+matStorage.getMsId());
		System.out.println("855"+metStorageVo.getMsId());
		metStorageVo.setMsType(matStorage.getMsType());
		metStorageVo.setMsVoucherId(matStorage.getMsVoucherId());
		metStorageVo.setMsDate(matStorage.getMsDate());
		if(matStorage.getMsOperator() !=null){
			metStorageVo.setMsOperator(matStorage.getMsOperator());
			Tbstaff staff = lmbtbstaffDAO.findOne(matStorage.getMsOperator().longValue());
			metStorageVo.setJbrName(staff.getStaffName());
		}else{
			metStorageVo.setMsOperator(null);
			metStorageVo.setJbrName(null);		
		}	
		metStorageVo.setMsStatus(matStorage.getMsStatus());
		YzrStorehouse storehouse = storehouseDAO.findOne(matStorage.getMsStatus());
		metStorageVo.setCkName(storehouse.getStoreName());
		return metStorageVo;		
	}
	
	public List<LmbMetStorageVo> metStorageListToVoList(List<LmbMaterialStorage> list){
		if(list == null) return null;
		List<LmbMetStorageVo> metStorageVoList = new ArrayList<>();
		for(LmbMaterialStorage materialStorage:list){
			System.out.println("3333");
			LmbMetStorageVo metStorageVo = metStoragePoToVo(materialStorage);
			System.out.println("111"+materialStorage.getMsId());			
			System.out.println("2222"+metStorageVo.getMsId());
			metStorageVoList.add(metStorageVo);
		}
		return metStorageVoList;		
	}
	
	public Map<String,Object> queryAllMaterialStorage(int page,int rows){
		PageRequest pr = new PageRequest(page - 1, rows);
		List<LmbMaterialStorage> materialStorage = lmbMatStorageDAO.findAllMaterialStorage(pr);
		for(LmbMaterialStorage MaterialStorage:materialStorage){
			System.out.println(MaterialStorage.getMsId());
		}
		List<LmbMetStorageVo> list =  metStorageListToVoList(materialStorage);
		for(LmbMetStorageVo metStorageVo:list){
			System.out.println("66666666666:"+metStorageVo.getMsId());
		}
		//获取总的记录条数
		long total = lmbMatStorageDAO.countAllMaterialStorage();
		System.out.println(total);
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows",list);
		return map;		
	}	
	
	public LmbMetStorageVo queryMaterialStorage(String msId){
		LmbMaterialStorage materialStorage = lmbMatStorageDAO.findMaterialStorage(msId);
		return metStoragePoToVo(materialStorage);		
	}
	
	public LmbMaterialStorageDetailVo MatStorageDetailPoToVo(LmbMaterialStorageDetail matStorageDetail){
		LmbMaterialStorageDetailVo matStorageDetailVo = new LmbMaterialStorageDetailVo();
		matStorageDetailVo.setMsdId(matStorageDetail.getMsdId());
		matStorageDetailVo.setMsdMsId(matStorageDetail.getMsdMsId());
		matStorageDetailVo.setMsdCId(matStorageDetail.getMsdCId());
		matStorageDetailVo.setMsdCName(matStorageDetail.getMsdCName());
		matStorageDetailVo.setMsdNorms(matStorageDetail.getMsdNorms());
		matStorageDetailVo.setMsdBatchNum(matStorageDetail.getMsdBatchNum());
		matStorageDetailVo.setMsdNum(matStorageDetail.getMsdNum());
		matStorageDetailVo.setMsdValidTill(matStorageDetail.getMsdValidTill());
		matStorageDetailVo.setMsdProductDate(matStorageDetail.getMsdProductDate());
		matStorageDetailVo.setMsdSupplierl(matStorageDetail.getMsdSupplierl());
		YzrSupplier supplier = supplierDAO.findOne(matStorageDetail.getMsdSupplierl());
		matStorageDetailVo.setGysName(supplier.getSupName());		
		return matStorageDetailVo;
	}
	
	public List<LmbMaterialStorageDetailVo> MatStorageDetailListToVoList(List<LmbMaterialStorageDetail> list){
		if(list==null) return null;
		List<LmbMaterialStorageDetailVo> matStorageDetailVo = new ArrayList<>();
		for(LmbMaterialStorageDetail matStorageDetail:list){
			LmbMaterialStorageDetailVo matStoDetailVo = MatStorageDetailPoToVo(matStorageDetail);
			matStorageDetailVo.add(matStoDetailVo);
		}
		return matStorageDetailVo;		
	}
	
	public List<LmbMaterialStorageDetailVo> findMatStorDetailsByMsdMsId(String msId){
		List<LmbMaterialStorageDetail> matStorageDetail = lmbMatStorageDetDAO.queryMatStorDetailsByMsdMsId(msId);
		return MatStorageDetailListToVoList(matStorageDetail);
	}
	
	public Map<String,Object> findMaterialStorageByDate(int page,int rows,Date ksrq,Date jsrq){
		PageRequest pr = new PageRequest(page - 1, rows);
		List<LmbMaterialStorage> materialStorage = lmbMatStorageDAO.findMaterialStorageByDate(pr,ksrq, jsrq);
		//获取总的记录条数
		long total = lmbMatStorageDAO.count();
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", metStorageListToVoList(materialStorage));
		return map;		
	}
	
	public LmbCousumablePurchase findCouPurById(String id){
		LmbCousumablePurchase LmbCousumablePurchase = couPurDAO.findOne(id);
		return LmbCousumablePurchase;	
				
	}
}
